#!/bin/bash
##定义了常用的字体效果及基础功能
##定义字体效果
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
##常用语句定义区
Entering(){
	echo -e "${green_bg}正在进入...${end}\n${red_zt}${1}...${end}"
}
red_z_green_z_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
check(){
	echo -e "${red_zt}正在检测${1}...${end}"
}
green_zt(){
	echo -e "${green_zt}${1}${end}"
}
red_z_green_z(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
red_z(){
	echo -e "${red_zt}${1}${end}"
}
red_bg(){
	echo -e "${red_bg}${1}${end}"
}
red_zt_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
}
install_check(){
    red_z_green_z_info "安装配置信息确认"
    input "当前主机名预配置为: $HOST_NAME\n按回车继续安装" "dd"
}
chek_status(){
    if [[ "$1" -eq '0' ]];then
        if [[  "$3" -eq 'q' ]];then
            return 0
        else
            red_z_green_z_info "$2" "成功"
        fi
        return 0
    else
        status='true'
        while $status;do
            red_zt_bg "$2" "失败"
            err='n'
            input "是否停止脚本运行[当前脚本PID: $$]\n输入f忽略该提示" "err"
            if [[ "$err" == 'f' ]];then
                status='false'
                if [[ "$status" == 'false' ]];then
                    red_z "已跳过该错误:[${2}失败]"
                else
                    status='true'
                fi
            else
                status=true
            fi
        done
    fi
}
disable_firealld(){
    systemctl stop firewalld
    systemctl disable firewalld
    setenforce 0
	if [[ -f "/etc/selinux/config" ]];then
			sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
	fi
	if [[ -f "/etc/sysconfig/selinux" ]];then
			sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
	fi
}
#服务循环安装
service_install(){
    for i in $*;do
        red_z_green_z_info "正在安装---->>>>" "${i}\t安装速度根据包的大小及网络而定,请耐心等待"
        red_z_green_z_info "当前脚本进程PID---->>>" "$$"
        if [[ "$display" == 'n' ]];then
            $install $i > /dev/null 2>&1
        else
            $install $i
        fi
        chek_status "$?" "${i}安装"
    done
}
#服务卸载
service_unstall(){
    input "即将卸载以下服务:\n$* \n是否全部卸载[y/n]" "re"
    for i in $*;do
        green_zt "$i"
    done
    if [[ "$re" == "y" || "$re" == "Y" ]];then
        for i in $*;do
            red_z_green_z_info "正在卸载---->>>>" "$i"
            $unstall $i
            chek_status "$?" "${i}卸载"
        done
    else
        input "是否自定义输入卸载[y/n]" "zre"
        if  [[ "$zre" == "y" || "$zre" == "Y" ]];then
            input "请输入需要卸载的包,并以空格分开" "sre"
            for i in $sre;do
                $unstall $i
            done
        else
            red_z "跳过卸载"
        fi
    fi
}
#服务启动
service_start(){
    systemctl daemon-reload
    for i in $*;do
        systemctl restart $i
        chek_status "$?" "${i}服务启动"
        systemctl enable $i > /dev/null
        chek_status "$?" "${i}设置服务开机启动"
    done
}
service_stop(){
    for i in $*;do
        systemctl stop $i
        chek_status "$?" "${i}服务关闭"
        systemctl disable $i > /dev/null
        chek_status "$?" "${i}关闭开机启动"
    done
}
service_chek(){
    red_z_green_z_info "正在检查" "$1"
    ser=`$local_search | grep $1`
    if [[ -n "$ser" ]];then
        input "检测到本机已安装[$1]相关服务,请选择一个操作\n[1]跳过此服务[默认选项]\n[2]卸载该服务" db_remove
        if [[ "$db_remove" == '2' ]];then
            service_unstall "$ser"
        else
            red_z_green_z_info "跳过卸载" "$1"
            db_init='y'
        fi
    fi
}
Check_Network(){
	check "网络连接"
	ping baidu.com -c 5 > /dev/null
	chek_status "$?" "DNS配置"
}
get_os_info_os-Type(){
    # for type in $*;do
    System_Type=`cat /etc/os-release  | grep ^ID= | sed -e 's/ID=//' -e 's/\"//g'`
    if [[ "${System_Type}" == "$type" ]];then
        os_check='y'
    fi
    if [[ ${System_Type} == 'centos' ]];then
        System_version=`cat /etc/os-release  | grep VERSION_ID | sed -e 's/VERSION_ID=//' -e 's/\"//g'`
    elif [[ ${System_Type} == 'debian' ]];then
        System_version=`grep VERSION_ID /etc/os-release  | sed -e 's/VERSION_ID=//' -e 's/\"//g'`
    fi
    if [[ "$os_check" -eq 'y' ]];then
        red_z_green_z "当前系统类型" "支持"
    else
        red_zt_bg "当前系统类型" "不支持"
        exit 11
    fi
}
get_os_info_os-Package-management(){
    if [[ -f '/usr/bin/apt' ]];then
        local_install='dpkg'
        Package_management='apt'
        Package_managements='apt'
        local_search="apt list --installed "
    elif [[ -f '/usr/bin/yum' ]];then
        yum --version > /dev/null
        if [[ "$?" -eq '0' ]];then
            Package_management='yum'
            Package_managements='yum'
            if [[ -f '/usr/bin/dnf' ]];then
                dnf --version > /dev/null
                if [[ "$?" -eq '0' ]];then
                    Package_management='dnf'
                fi
            fi
            local_search='rpm -qa '
        else
            red_zt_bg "包管理器[ yum ]" "出现意外"
            exit 11
        fi
        local_install='rpm'
    else
        red_zt_bg "无法判断当前系统使用的包管理器"
        exit 11
    fi
    export install="${Package_management} install -y"
    export unstall="${Package_management} remove -y"
}
#1参是镜像名称
#2参是镜像URL的变量名
source_menu-mirrors-select(){
    input "请选择${1}镜像站
[1]华为源(建议)
[2]腾讯源
[3]阿里源
[4]网易源
[5]中国科技技术大学" URL_select
    if [[ "$URL_select" == '1' ]];then
    	URL='https://mirrors.huaweicloud.com'
        mirrors_name='华为源'
    elif [[ "$URL_select" == '2' ]];then
    	URL='https://mirrors.cloud.tencent.com'
        mirrors_name="[ 腾讯源 ]"
    elif [[ "$URL_select" == '3' ]];then
        URL='https://mirrors.aliyun.com'
        mirrors_name="[ 阿里源 ]"
    elif [[  "$URL_select" == '4' ]];then
    	URL='https://mirrors.huaweicloud.com'
        mirrors_name="[ 网易源 ]"
    elif [[  "$URL_select" == '5' ]];then
    	URL='https://mirrors.ustc.edu.cn'
        mirrors_name="[ 华为源 ]"
    else
        URL='https://mirrors.huaweicloud.com'
        mirrors_name='华为源'
    fi
    eval `echo -e "$2=$URL"`
    red_z_green_z "${1}镜像源已选择:" "[ $mirrors_name ]"
}

source_apt_debian(){
    cat <<EOF > /etc/apt/sources.list 
deb http://mirrors.ustc.edu.cn/debian/ buster main
deb-src http://mirrors.ustc.edu.cn/debian/ buster main

deb http://security.debian.org/debian-security buster/updates main
deb-src http://security.debian.org/debian-security buster/updates main


deb http://mirrors.ustc.edu.cn/debian/ buster-updates main
deb-src http://mirrors.ustc.edu.cn/debian/ buster-updates main

deb http://mirrors.ustc.edu.cn/debian/ buster-backports main non-free contrib
deb-src http://mirrors.ustc.edu.cn/debian/ buster-backports main non-free contrib
EOF
case $source_version in
    '7') sed -i 's/buster/wheezy/g' /etc/apt/sources.list
        ;;
    '8') sed -i 's/buster/jessie/g' /etc/apt/sources.list
        ;;
    '9') sed -i 's/buster/stretch/g' /etc/apt/sources.list
        ;;
esac
    apt update
    service_install 'apt-transport-https ca-certificates  gnupg gnupg2 curl gnupg2 software-properties-common sudo'
    ##判断是否通过1参传入镜像站替换
	if [[ -n "$1" ]];then
		sed -i 's#http://mirrors.ustc.edu.cn#$1#g' /etc/apt/sources.list
        sed -i 's#http://security.debian.org#$1#g' /etc/apt/sources.list
	else
		green_zt "[ Debian${System_version} ]使用中国科技大学镜像站"
	fi
}
source_apt_ubuntu(){
    cat <<EOF > /etc/apt/sources.list
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
EOF
#通过判断系统版本替换源内容即可实现(待完善)
case $source_version in
    '14') sed -i 's/focal/trusty/g' /etc/apt/sources.list
        ;;
    '16') sed -i 's/focal/xenial/g' /etc/apt/sources.list
        ;;
    '18') sed -i 's/focal/bionic/g' /etc/apt/sources.list
        ;;
esac
}
##备份源
source_apt_bak(){
    mkdir -p /etc/apt/sources.list.d/
    if [[ -f '/etc/apt/sources.list.bak' ]];then
        green_zt "文件[ /etc/apt/sources.list.bak ]已备份"
        rm -f /etc/apt/sources.list
    else
        cp /etc/apt/sources.list /etc/apt/sources.list.bak
        rm -f /etc/apt/sources.list
    fi
}
source_apt_docker-ce(){
    service_install " gnupg gnupg2 "
    rm -f gpg
    curl -fsSL https://mirrors.huaweicloud.com/docker-ce/linux/${System_Type}/gpg | apt-key add -
    rm -f gpg
    curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/${System_Type}/gpg | apt-key add -
    add-apt-repository "deb [arch=amd64] https://mirrors.huaweicloud.com/docker-ce/linux/${System_Type} $(lsb_release -cs) stable"
    ##判断是否通过1参传入镜像站替换
	if [[ -n "$1" ]];then
		sed -i "s#https://mirrors.huaweicloud.com/docker-ce/#${1}/docker-ce/#g" /etc/apt/sources.list
	else
        sed -i 's#https://mirrors.huaweicloud.com/docker-ce/#https://mirrors.aliyun.com/docker-ce/#g' /etc/apt/sources.list
		green_zt "[ Docker-ce源 ]使用阿里云镜像站"
	fi
    # sed -i "s#/docker-ce/linux/debian#/docker-ce/linux/${System_Type}#g" /etc/apt/sources.list
}
##源缓存建立
makecache(){
	yum clean all 
	yum makecache
	chek_status "$?" "$1"
	service_install 'yum-utils device-mapper-persistent-data lvm2'
}
#源配置文件备份检测
source_yum_bak(){
	if [[ -d "/yum_bak/" ]];then
		green_zt "源文件已备份"
		rm -f /etc/yum.repos.d/*
	else
		green_zt "正在备份源文件"
		mkdir -p /yum_bak/
		mv /etc/yum.repos.d/* /yum_bak/
		red_z_green_z_info "源备份" "[SCUUESS]"
	fi
}
#已确定可用(CentOS7-Base源)
source_yum_centos7(){
echo '[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/os/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/updates/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras 
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/
gpgcheck=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-7' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos7系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	makecache "centos7系统源配置"
}
source_yum_centos8(){
echo '
[base]
name=CentOS-$releasever - Base
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/BaseOS/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/extras/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
failovermethod=priority
baseurl=https://mirrors.huaweicloud.comcentos/$releasever/centosplus/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official
 
[PowerTools]
name=CentOS-$releasever - PowerTools
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/PowerTools/$basearch/os/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official


[AppStream]
name=CentOS-$releasever - AppStream
failovermethod=priority
baseurl=https://mirrors.huaweicloud.com/centos/$releasever/AppStream/$basearch/os/
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/centos/RPM-GPG-KEY-CentOS-Official' > /etc/yum.repos.d/CentOS-Base.repo
	chek_status "$?" "centos8系统源配置文件写入"
	##判断是否通过1参传入镜像站替换
	makecache "系统源配置"
}
#已确定可用(docker-ce源)
source_yum_docker-ce(){
echo '[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.huaweicloud.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.huaweicloud.com/docker-ce/linux/centos/gpg
' >  /etc/yum.repos.d/Docker.repo
	sed -i "s#/7/#/${System_version}/#g" /etc/yum.repos.d/Docker.repo
	makecache "Dcoker源配置"
}
install_docker-ce_install-docker(){
	Entering "Docker安装..."
	srv='docker-ce'
	if [[ "$Package_managements" == 'apt' ]];then
		srv='docker.io'
		echo '暂时使用io'
	elif [[ "$Package_managements" == 'yum' ]];then
		srv='docker-ce'
		service_chek "$srv"
		if [[ "$System_version" == '7' ]];then
			re=`yum list docker-ce --showduplicates|sort -r | awk '{print $2}' | awk 'NR>6{print}'|sed 's/base://'| awk NF |sed 's/3://'`
			for i in $re;do
				green_zt $i
			done
			green_zt "请选择一个docker版本并粘贴\n默认安装最新版"
			input "低版本很有可能安装失败,建议安装较新版本" "docker_ver"
			if [[ -z "$docker_ver" ]];then
				green_zt "已选择安装最新版本"
				dockers="$srv"
			else
				if [[ -z `echo $re | grep "$docker_ver"` ]];then
					red_bg "好像你输入的版本并不在上面的列表中"
					green_zt "已默认安装最新版"
				else
					green_zt "已选择安装${docker_ver}"
					srv="${srv}-${docker_ver}"
				fi
			fi
		fi
		Entering "Docker安装..."
	fi
	service_install "$srv" 
	chek_status "$?" "${srv}安装"
}
install_docker-ce_docker-config(){
	mkdir -p  /etc/docker/
	echo -e "{\n\t\"exec-opts\": [\"native.cgroupdriver=systemd\"],\n\t\"registry-mirrors\": [\"https://ung2thfc.mirror.aliyuncs.com\"]\n}" > /etc/docker/daemon.json
	service_start "docker"
	chek_status "$?" "Docker镜像加速地址配置"
	install_docker-ce_get_docker_version
	green_zt "[ Docker ]安装成功"
}
install_docker-ce_get_docker_version(){
    Dcoker_C_ver=`docker version | grep Version | awk '{print $2}' |sed -n 1p`
    Dcoker_S_ver=`docker version | grep Version | awk '{print $2}' |sed -n 2p`
	red_z_green_z "Docker客户端版本:" "[ $Dcoker_C_ver ]"
	red_z_green_z "Docker服务端版本:" "[ $Dcoker_S_ver ]"
}
mirrors-selects(){
    if [[ "$source_select" == 'y' || "$source_select" == 'Y' ]];then
        source_menu-mirrors-select "$1" "$2"
    fi
}
install_menu_install-docker-ce-mini(){
    Check_Network #检测网络连接

    get_os_info_os-Type #获取系统版本信息

    get_os_info_os-Package-management #获取包管理器信息

    service_chek 'docker' 
    source_${Package_managements}_bak #检测源备份

    if [[ -f '/usr/sbin/apt' ]];then
        source_${Package_managements}_${System_Type}
    else
        source_${Package_managements}_${System_Type}${System_version}
    fi
    source_${Package_managements}_docker-ce 

    
    install_docker-ce_install-docker #安装Docker
    
    install_docker-ce_docker-config #配置Docker
    install_docker-ce_get_docker_version #获取Docker版本
}
install_menu_install-docker-ce-mini #执行安装程序